package com.google.code.jmarias.client.components;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Komponenta obstaravajici vyber karty.
 * Necha hrace pomoci klavesnice vybrat kartu a po stisknuti ENTERu ji vyjede.
 * 
 * @author bart
 */
public class PickCardComponent extends AbstractRenderComponent {
    private GameLocals locals;
    private Globals globals;

    // Pozice karet
    private ArrayList<Vector2f> cardsPositions;

    // Index vybrane karty
    private Integer keyboardPointer = 0;

    // Indikator zda jeste hrac vybira nebo jiz bezi animace vyjeti karty
    private boolean animStart = false;

    private ArrayList<Entity> cardsEntities;


    public PickCardComponent(int id, Globals globals, GameLocals locals, ArrayList<Vector2f> cardsPositions, ArrayList<Entity> cardsEntities) throws SlickException {
        super(id);
        this.cardsPositions = cardsPositions;
        this.locals = locals;
        this.globals = globals;
        this.cardsEntities = cardsEntities;

//        if(! locals.getPickedCards().isEmpty()) {
//              keyboardPointer = (locals.getPickedCards().get(locals.getPickedCards().size() - 1).getCardIndex() + cardsEntities.size() + 1) % cardsEntities.size();
//        }

    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
      if(delta < 1000) {
        Input input = gc.getInput();

        // Hrac teprve vybira kartu pomoci sipek na klavesnici
        if(! animStart) {

            if(! locals.getPickedCards().isEmpty() && locals.getPickedCards().get(locals.getPickedCards().size() - 1).getCardIndex() == keyboardPointer) {
                    keyboardPointer = (keyboardPointer + cardsEntities.size() + 1) % cardsEntities.size();
            }
            // Vykresleni karet na jejich pozicich (jiz vybrane karty jsou o trosku vys)
            for(int i=0; i<cardsEntities.size(); i++) {
                if(locals.getPickedCards().contains(locals.getCardsEntities().get(i))) {
                    cardsEntities.get(i).setPosition(new Vector2f(cardsPositions.get(i).x, cardsPositions.get(i).y - 20));
                } else {
                    cardsEntities.get(i).setPosition(cardsPositions.get(i));
                }
                
            }
            
            // Obsluha klavesnice - pomoci sipek hrac vybira kartu, ENTER kartu odesle
            if(input.isKeyPressed(Input.KEY_RIGHT)) {
                  keyboardPointer = (keyboardPointer + cardsEntities.size() + 1) % cardsEntities.size();
                  if(! locals.getPickedCards().isEmpty() && locals.getPickedCards().get(locals.getPickedCards().size() - 1).getCardIndex() == keyboardPointer) {
                    keyboardPointer = (keyboardPointer + cardsEntities.size() + 1) % cardsEntities.size();
                  }
            } else if(input.isKeyPressed(Input.KEY_LEFT)) {
                  keyboardPointer = (keyboardPointer + cardsEntities.size() - 1) % cardsEntities.size();
                  if(! locals.getPickedCards().isEmpty() && locals.getPickedCards().get(locals.getPickedCards().size() - 1).getCardIndex() == keyboardPointer) {
                    keyboardPointer = (keyboardPointer + cardsEntities.size() - 1) % cardsEntities.size();
                  }
            } else if(globals.getHoverID() == 0 && input.isKeyPressed(Input.KEY_ENTER)) {
                 // Konec vybirani
                System.out.println("Vybirani - konec");
                locals.getPickedCards().add(cardsEntities.get(keyboardPointer));
                animStart = true;              
            }

            // Umisteni vybiraciho ramecku
            locals.getFrameEntity().setPosition(cardsEntities.get(keyboardPointer).getPosition());

        } else {
        // Hrac vybral kartu, spusti se animace vyjeti vybrane karty

        for(int i = 0; i < cardsEntities.size(); i++) {
            if(locals.getPickedCards().contains(locals.getCardsEntities().get(i))) {
                if(cardsEntities.get(i).getPosition().y > cardsPositions.get(i).y - 20) {
                    cardsEntities.get(i).setPosition(new Vector2f(cardsEntities.get(i).getPosition().x,
                                                                  cardsEntities.get(i).getPosition().y - delta * 0.3f));
                } else {
                    cardsEntities.get(i).setPosition(new Vector2f(cardsEntities.get(i).getPosition().x,
                                                                  cardsPositions.get(i).y - 20));

                    if(locals.getCardsEntities().get(i) == locals.getPickedCards().get(locals.getPickedCards().size() - 1)) {
                        // Ukonceni vybirani - smaze komponentu
                        System.out.println("Ukaz vybranou kartu - konec.");
                        owner.DelComponent(id);
                    }
                }
            }
        }

        }

      }
    }


    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
        for(int i = 0; i < cardsEntities.size(); i++) {
            cardsEntities.get(i).render(gc, sb, gr);
        }

        if(! animStart && globals.getHoverID() == 0)
            locals.getFrameEntity().render(gc, sb, gr);
    }

}
